package com.ruoyi.member.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.member.entity.req.WxMiniLoginReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序服务http
 */
@Service
@Slf4j
public class WeChatMiniHttp {

    @Value("${wechat.mini.appId}")
    private String appId;
    @Value("${wechat.mini.appSecret}")
    private String appSecret;
    @Value("${wechat.mini.publicKey}")
    private String publicKey;
    @Value("${wechat.mini.privateKey}")
    private String privateKey;

    private static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";


    /**
     * 小程序登录
     */
    public JSONObject login(WxMiniLoginReq req) {

        Map<String, Object> map = new HashMap<>();
        map.put("appid", appId);
        map.put("secret", appSecret);
        map.put("js_code", req.getCode());
        map.put("grant_type", "authorization_code");

        String info = HttpUtil.get(WX_LOGIN, map);

        log.info("微信小程序登录http response：{}", info);

        return JSONObject.parseObject(info);
    }

}
